package com.k12.admim.controller.school;

import com.k12.admim.model.entity.edu.SchoolInfo;
import com.k12.admim.model.entity.sys.SysRole;
import com.k12.admim.model.vo.edu.SchoolInfoVo;
import com.k12.admim.rpc.EduManageFeignClient;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import com.k12.common.util.PageUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class SchoolController {

    @Resource
    private EduManageFeignClient eduManageFeignClient;



    @GetMapping("campus/school/list")
    public R list(SchoolInfoVo schoolInfoVo) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        List<SchoolInfo> list = eduManageFeignClient.selectSchoolInfoList(schoolInfoVo);
        PageUtil.PageResult<SchoolInfo> result = PageUtil.paginate(list, schoolInfoVo.getPageNum(), schoolInfoVo.getPageSize());
        return R.success(result);
    }

    @GetMapping("campus/school/{id}")
    public R list(@PathVariable("id") Long id) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        SchoolInfoVo schoolInfoVo = new SchoolInfoVo();
        schoolInfoVo.setId(id);
        schoolInfoVo.setTenantId(Long.valueOf(tenantId));
        List<SchoolInfo> list = eduManageFeignClient.selectSchoolInfoList(schoolInfoVo);
        return R.success(list.get(0));
    }


    @PostMapping("campus/school")
    public R add(@RequestBody SchoolInfoVo schoolInfoVo) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        SchoolInfoVo schoolInfoVos = new SchoolInfoVo();
        schoolInfoVos.setTenantId(Long.valueOf(tenantId));
        schoolInfoVos.setName(schoolInfoVos.getName());
        List<SchoolInfo> list = eduManageFeignClient.selectSchoolInfoList(schoolInfoVos);
        SchoolInfo schoolInfo = list.get(0);
        if (schoolInfo != null){
            return R.failed("学校名称已存在");
        }
        eduManageFeignClient.insertSchoolInfo(schoolInfoVo);
        return R.success(true);
    }


    @DeleteMapping("campus/school/{ids}")
    public R deleteSchool(@PathVariable List<Long> ids) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        for (Long id : ids){
            eduManageFeignClient.deleteSchoolInfo(id,tenantId);
        }
        return R.success(true);
    }


    @PutMapping("campus/school")
    public R edit(@RequestBody SchoolInfoVo schoolInfoVo) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        return R.success(eduManageFeignClient.updateSchoolInfo(schoolInfoVo));
    }



}
